<html>
<head><meta charset="utf-8"><title>Always uninitialized · t-lang/wg-unsafe-code-guidelines · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/index.html">t-lang/wg-unsafe-code-guidelines</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Always.20uninitialized.html">Always uninitialized</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="172304039"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Always%20uninitialized/near/172304039" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Always.20uninitialized.html#172304039">(Aug 02 2019 at 12:29)</a>:</h4>
<p>Can I use:</p>
<div class="codehilite"><pre><span></span><span class="k">type</span> <span class="nc">AlwaysUninit</span><span class="o">&lt;</span><span class="n">T</span><span class="o">&gt;</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">MaybeUninit</span><span class="o">&lt;</span><span class="p">(</span><span class="n">T</span><span class="p">,</span><span class="w"> </span><span class="o">!</span><span class="p">)</span><span class="o">&gt;</span><span class="p">;</span><span class="w"></span>
</pre></div>


<p>as a safe Always  Uninitialized wrapper over a T ?</p>



<a name="172304211"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Always%20uninitialized/near/172304211" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Hanna Kruppe <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Always.20uninitialized.html#172304211">(Aug 02 2019 at 12:32)</a>:</h4>
<p>If you mean that nobody can ever (safely) write anything to the <code>T</code> bytes, then no. For example, with sufficient placement/RVO smarts, <code>let x: AlwaysUninit&lt;String&gt; = ("foo".into(), panic!())</code> not only writes a <code>String</code> to <code>x</code>, it even cleans it up afterwards.</p>



<a name="172304245"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Always%20uninitialized/near/172304245" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Always.20uninitialized.html#172304245">(Aug 02 2019 at 12:33)</a>:</h4>
<p>I only want to prevent reads via <code>MaybeUninit::assume_init</code></p>



<a name="172304256"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Always%20uninitialized/near/172304256" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Always.20uninitialized.html#172304256">(Aug 02 2019 at 12:33)</a>:</h4>
<p>If someone wants to do anything more complex to actually read or write to the bytes, they should be able to</p>



<a name="172304928"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Always%20uninitialized/near/172304928" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nagisa <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Always.20uninitialized.html#172304928">(Aug 02 2019 at 12:44)</a>:</h4>
<p>Yeah, it is valid and the call will panic</p>



<a name="172304963"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Always%20uninitialized/near/172304963" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nagisa <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Always.20uninitialized.html#172304963">(Aug 02 2019 at 12:45)</a>:</h4>
<p>Many <code>MaybeUninit</code> by-value methods should compile down to a panic when its <code>T</code> is uninhabited, which is true in this case.</p>



<a name="172305045"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Always%20uninitialized/near/172305045" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nagisa <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Always.20uninitialized.html#172305045">(Aug 02 2019 at 12:46)</a>:</h4>
<p>Now this made me wonder… did we reach a decision on unhabitedness of <code>&amp;!</code>?</p>



<a name="172306776"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Always%20uninitialized/near/172306776" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Always.20uninitialized.html#172306776">(Aug 02 2019 at 13:09)</a>:</h4>
<blockquote>
<p>Now this made me wonder… did we reach a decision on unhabitedness of &amp;!?</p>
</blockquote>
<p>No, that is <a href="https://github.com/rust-lang/unsafe-code-guidelines/issues/77" target="_blank" title="https://github.com/rust-lang/unsafe-code-guidelines/issues/77">https://github.com/rust-lang/unsafe-code-guidelines/issues/77</a></p>



<a name="172322283"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Always%20uninitialized/near/172322283" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Always.20uninitialized.html#172322283">(Aug 02 2019 at 16:27)</a>:</h4>
<p><span class="user-mention" data-user-id="132920">@gnzlbg</span> I do not understand what you are trying to achieve</p>



<a name="172322311"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Always%20uninitialized/near/172322311" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Always.20uninitialized.html#172322311">(Aug 02 2019 at 16:27)</a>:</h4>
<p><code>assume_init</code> is unsafe so if you are worried about a safe wrapper, you can ignore it</p>



<a name="172412615"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Always%20uninitialized/near/172412615" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Always.20uninitialized.html#172412615">(Aug 03 2019 at 16:35)</a>:</h4>
<p>I actually needed a type to express that something is always uninitialized</p>



<a name="172412659"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Always%20uninitialized/near/172412659" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Hanna Kruppe <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Always.20uninitialized.html#172412659">(Aug 03 2019 at 16:37)</a>:</h4>
<p>You said so from the start but it's not clear what that means or what you want it for (I know from discord you were thinking about manually adding padding to a type but what exactly do you intend to accomplish by "expressing" this?)</p>



<a name="172415099"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Always%20uninitialized/near/172415099" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Always.20uninitialized.html#172415099">(Aug 03 2019 at 17:49)</a>:</h4>
<p>Its "explicit" padding for <code>repr(C)</code> types, there is never a need to initialize that to anything, nor to try to <code>::assume_init</code> it, so none of that is needed.</p>



<a name="172416741"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Always%20uninitialized/near/172416741" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Always.20uninitialized.html#172416741">(Aug 03 2019 at 18:41)</a>:</h4>
<p>sounds like you just want a newtype around <code>[MaybeUninit&lt;u8&gt;; N]</code>?</p>



<a name="172416742"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Always%20uninitialized/near/172416742" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Always.20uninitialized.html#172416742">(Aug 03 2019 at 18:41)</a>:</h4>
<p>no type will have a validity invariant "must be uninitialized"</p>



<a name="172416745"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Always%20uninitialized/near/172416745" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Always.20uninitialized.html#172416745">(Aug 03 2019 at 18:42)</a>:</h4>
<p>and the safety invariant is up to you with a newtype</p>



<a name="172416786"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Always%20uninitialized/near/172416786" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Always.20uninitialized.html#172416786">(Aug 03 2019 at 18:42)</a>:</h4>
<p>but I also cannot imagine a situation where uninitialized memory is okay but initialized memory is not</p>



<a name="172416788"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Always%20uninitialized/near/172416788" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Always.20uninitialized.html#172416788">(Aug 03 2019 at 18:42)</a>:</h4>
<p>this includes padding</p>



<a name="172416793"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Always%20uninitialized/near/172416793" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Always.20uninitialized.html#172416793">(Aug 03 2019 at 18:42)</a>:</h4>
<p>in which case plain <code>[MaybeUninit&lt;u8&gt;; N]</code> would do it</p>



<a name="172486730"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Always%20uninitialized/near/172486730" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Always.20uninitialized.html#172486730">(Aug 05 2019 at 08:14)</a>:</h4>
<p><span class="user-mention" data-user-id="120791">@RalfJ</span> I just ended with a wrapper <code>struct Padding&lt;T&gt;(MaybeUninit&lt;T&gt;);</code> that does not expose the private field, has no <code>assume_init</code>, etc.</p>



<a name="172519591"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Always%20uninitialized/near/172519591" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Always.20uninitialized.html#172519591">(Aug 05 2019 at 16:57)</a>:</h4>
<p>seems reasonable</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>